package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.CureTaskCourseDAO;
import cn.edu.scau.cmi.hombio.dao.CureTaskMedicineDAO;
import cn.edu.scau.cmi.hombio.dao.CureTaskMethodDAO;
import cn.edu.scau.cmi.hombio.dao.CureTypeDAO;

import cn.edu.scau.cmi.hombio.domain.CureTaskCourse;
import cn.edu.scau.cmi.hombio.domain.CureTaskMedicine;
import cn.edu.scau.cmi.hombio.domain.CureTaskMethod;
import cn.edu.scau.cmi.hombio.domain.CureType;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for CureTaskMethod entities
 * 
 */

@Service("CureTaskMethodService")

@Transactional
public class CureTaskMethodServiceImpl implements CureTaskMethodService {

	/**
	 * DAO injected by Spring that manages CureTaskCourse entities
	 * 
	 */
	@Autowired
	private CureTaskCourseDAO cureTaskCourseDAO;

	/**
	 * DAO injected by Spring that manages CureTaskMedicine entities
	 * 
	 */
	@Autowired
	private CureTaskMedicineDAO cureTaskMedicineDAO;

	/**
	 * DAO injected by Spring that manages CureTaskMethod entities
	 * 
	 */
	@Autowired
	private CureTaskMethodDAO cureTaskMethodDAO;

	/**
	 * DAO injected by Spring that manages CureType entities
	 * 
	 */
	@Autowired
	private CureTypeDAO cureTypeDAO;

	/**
	 * Instantiates a new CureTaskMethodServiceImpl.
	 *
	 */
	public CureTaskMethodServiceImpl() {
	}

	/**
	 * Delete an existing CureType entity
	 * 
	 */
	@Transactional
	public CureTaskMethod deleteCureTaskMethodCureType(Integer curetaskmethod_id, Integer related_curetype_id) {
		CureTaskMethod curetaskmethod = cureTaskMethodDAO.findCureTaskMethodByPrimaryKey(curetaskmethod_id, -1, -1);
		CureType related_curetype = cureTypeDAO.findCureTypeByPrimaryKey(related_curetype_id, -1, -1);

		curetaskmethod.setCureType(null);
		related_curetype.getCureTaskMethods().remove(curetaskmethod);
		curetaskmethod = cureTaskMethodDAO.store(curetaskmethod);
		cureTaskMethodDAO.flush();

		related_curetype = cureTypeDAO.store(related_curetype);
		cureTypeDAO.flush();

		cureTypeDAO.remove(related_curetype);
		cureTypeDAO.flush();

		return curetaskmethod;
	}

	/**
	 * Save an existing CureTaskMethod entity
	 * 
	 */
	@Transactional
	public void saveCureTaskMethod(CureTaskMethod curetaskmethod) {
		CureTaskMethod existingCureTaskMethod = cureTaskMethodDAO.findCureTaskMethodByPrimaryKey(curetaskmethod.getId());

		if (existingCureTaskMethod != null) {
			if (existingCureTaskMethod != curetaskmethod) {
				existingCureTaskMethod.setId(curetaskmethod.getId());
				existingCureTaskMethod.setMemo(curetaskmethod.getMemo());
			}
			curetaskmethod = cureTaskMethodDAO.store(existingCureTaskMethod);
		} else {
			curetaskmethod = cureTaskMethodDAO.store(curetaskmethod);
		}
		cureTaskMethodDAO.flush();
	}

	/**
	 * Delete an existing CureTaskMedicine entity
	 * 
	 */
	@Transactional
	public CureTaskMethod deleteCureTaskMethodCureTaskMedicines(Integer curetaskmethod_id, Integer related_curetaskmedicines_id) {
		CureTaskMedicine related_curetaskmedicines = cureTaskMedicineDAO.findCureTaskMedicineByPrimaryKey(related_curetaskmedicines_id, -1, -1);

		CureTaskMethod curetaskmethod = cureTaskMethodDAO.findCureTaskMethodByPrimaryKey(curetaskmethod_id, -1, -1);

		related_curetaskmedicines.setCureTaskMethod(null);
		curetaskmethod.getCureTaskMedicines().remove(related_curetaskmedicines);

		cureTaskMedicineDAO.remove(related_curetaskmedicines);
		cureTaskMedicineDAO.flush();

		return curetaskmethod;
	}

	/**
	 * Delete an existing CureTaskMethod entity
	 * 
	 */
	@Transactional
	public void deleteCureTaskMethod(CureTaskMethod curetaskmethod) {
		cureTaskMethodDAO.remove(curetaskmethod);
		cureTaskMethodDAO.flush();
	}

	/**
	 * Load an existing CureTaskMethod entity
	 * 
	 */
	@Transactional
	public Set<CureTaskMethod> loadCureTaskMethods() {
		return cureTaskMethodDAO.findAllCureTaskMethods();
	}

	/**
	 * Save an existing CureTaskCourse entity
	 * 
	 */
	@Transactional
	public CureTaskMethod saveCureTaskMethodCureTaskCourse(Integer id, CureTaskCourse related_curetaskcourse) {
		CureTaskMethod curetaskmethod = cureTaskMethodDAO.findCureTaskMethodByPrimaryKey(id, -1, -1);
		CureTaskCourse existingcureTaskCourse = cureTaskCourseDAO.findCureTaskCourseByPrimaryKey(related_curetaskcourse.getId());

		// copy into the existing record to preserve existing relationships
		if (existingcureTaskCourse != null) {
			existingcureTaskCourse.setId(related_curetaskcourse.getId());
			existingcureTaskCourse.setStep(related_curetaskcourse.getStep());
			existingcureTaskCourse.setTime(related_curetaskcourse.getTime());
			existingcureTaskCourse.setMemo(related_curetaskcourse.getMemo());
			related_curetaskcourse = existingcureTaskCourse;
		}

		curetaskmethod.setCureTaskCourse(related_curetaskcourse);
		related_curetaskcourse.getCureTaskMethods().add(curetaskmethod);
		curetaskmethod = cureTaskMethodDAO.store(curetaskmethod);
		cureTaskMethodDAO.flush();

		related_curetaskcourse = cureTaskCourseDAO.store(related_curetaskcourse);
		cureTaskCourseDAO.flush();

		return curetaskmethod;
	}

	/**
	 * Save an existing CureType entity
	 * 
	 */
	@Transactional
	public CureTaskMethod saveCureTaskMethodCureType(Integer id, CureType related_curetype) {
		CureTaskMethod curetaskmethod = cureTaskMethodDAO.findCureTaskMethodByPrimaryKey(id, -1, -1);
		CureType existingcureType = cureTypeDAO.findCureTypeByPrimaryKey(related_curetype.getId());

		// copy into the existing record to preserve existing relationships
		if (existingcureType != null) {
			existingcureType.setId(related_curetype.getId());
			existingcureType.setName(related_curetype.getName());
			existingcureType.setMemo(related_curetype.getMemo());
			related_curetype = existingcureType;
		}

		curetaskmethod.setCureType(related_curetype);
		related_curetype.getCureTaskMethods().add(curetaskmethod);
		curetaskmethod = cureTaskMethodDAO.store(curetaskmethod);
		cureTaskMethodDAO.flush();

		related_curetype = cureTypeDAO.store(related_curetype);
		cureTypeDAO.flush();

		return curetaskmethod;
	}

	/**
	 * Save an existing CureTaskMedicine entity
	 * 
	 */
	@Transactional
	public CureTaskMethod saveCureTaskMethodCureTaskMedicines(Integer id, CureTaskMedicine related_curetaskmedicines) {
		CureTaskMethod curetaskmethod = cureTaskMethodDAO.findCureTaskMethodByPrimaryKey(id, -1, -1);
		CureTaskMedicine existingcureTaskMedicines = cureTaskMedicineDAO.findCureTaskMedicineByPrimaryKey(related_curetaskmedicines.getId());

		// copy into the existing record to preserve existing relationships
		if (existingcureTaskMedicines != null) {
			existingcureTaskMedicines.setId(related_curetaskmedicines.getId());
			existingcureTaskMedicines.setQuantity(related_curetaskmedicines.getQuantity());
			existingcureTaskMedicines.setMemo(related_curetaskmedicines.getMemo());
			related_curetaskmedicines = existingcureTaskMedicines;
		}

		related_curetaskmedicines.setCureTaskMethod(curetaskmethod);
		curetaskmethod.getCureTaskMedicines().add(related_curetaskmedicines);
		related_curetaskmedicines = cureTaskMedicineDAO.store(related_curetaskmedicines);
		cureTaskMedicineDAO.flush();

		curetaskmethod = cureTaskMethodDAO.store(curetaskmethod);
		cureTaskMethodDAO.flush();

		return curetaskmethod;
	}

	/**
	 */
	@Transactional
	public CureTaskMethod findCureTaskMethodByPrimaryKey(Integer id) {
		return cureTaskMethodDAO.findCureTaskMethodByPrimaryKey(id);
	}

	/**
	 * Return all CureTaskMethod entity
	 * 
	 */
	@Transactional
	public List<CureTaskMethod> findAllCureTaskMethods(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<CureTaskMethod>(cureTaskMethodDAO.findAllCureTaskMethods(startResult, maxRows));
	}

	/**
	 * Return a count of all CureTaskMethod entity
	 * 
	 */
	@Transactional
	public Integer countCureTaskMethods() {
		return ((Long) cureTaskMethodDAO.createQuerySingleResult("select count(o) from CureTaskMethod o").getSingleResult()).intValue();
	}

	/**
	 * Delete an existing CureTaskCourse entity
	 * 
	 */
	@Transactional
	public CureTaskMethod deleteCureTaskMethodCureTaskCourse(Integer curetaskmethod_id, Integer related_curetaskcourse_id) {
		CureTaskMethod curetaskmethod = cureTaskMethodDAO.findCureTaskMethodByPrimaryKey(curetaskmethod_id, -1, -1);
		CureTaskCourse related_curetaskcourse = cureTaskCourseDAO.findCureTaskCourseByPrimaryKey(related_curetaskcourse_id, -1, -1);

		curetaskmethod.setCureTaskCourse(null);
		related_curetaskcourse.getCureTaskMethods().remove(curetaskmethod);
		curetaskmethod = cureTaskMethodDAO.store(curetaskmethod);
		cureTaskMethodDAO.flush();

		related_curetaskcourse = cureTaskCourseDAO.store(related_curetaskcourse);
		cureTaskCourseDAO.flush();

		cureTaskCourseDAO.remove(related_curetaskcourse);
		cureTaskCourseDAO.flush();

		return curetaskmethod;
	}
}
